<template>
    <view>
        <!-- #ifdef MP-WEIXIN -->
        <view v-if="isCanUse">
            <view>
                <view class='header'>
                    <image src='../../../static/wechat.png'></image>
                </view>
                <view class='content'>
                    <view>申请获取以下权限</view>
                    <text>获得你的公开信息(昵称，头像、地区等)</text>
                </view>

                <button  style="background-color: #E02E24;" class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">
                    授权登录
                </button>
				<!-- <button style="background-color: #E02E24;" class='bottom' type="primary" open-type="getPhoneNumber" @getPhoneNumber="getPhoneNumber">获取微信绑定手机号</button> -->
            </view>
        </view>
        <!-- #endif -->
    </view>
</template>

<script>
	import {mapMutations} from 'vuex';
	export default {
		data() {
			return {
				SessionKey: '',
				openId: '',
				nickName: null,
				sex:null,
				province:null,
				city:null,
				country:null,
				avatarUrl: null,
				isCanUse: uni.getStorageSync('isCanUse')||true,//默认为true
			};
		},
		onLoad() {//默认加载
			this.login1();
		},
		methods: {
			...mapMutations(['login']),
			//第一授权获取用户信息===》按钮触发
			wxGetUserInfo() {
				let _this = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						_this.nickName = infoRes.userInfo.nickName; //昵称
						_this.sex = infoRes.userInfo.gender;
						_this.province = infoRes.userInfo.province;
						_this.city = infoRes.userInfo.city;
						_this.country = infoRes.userInfo.country;
						_this.avatarUrl = infoRes.userInfo.avatarUrl; //头像
						try {
							uni.setStorageSync('isCanUse', false);//记录是否第一次授权  false:表示不是第一次授权
							_this.updateUserInfo();
							
						} catch (e) {
							console.log(e)
						}
					},
					fail(res) {}
				});
			},
			getPhoneNumber(){
				uni.getPhoneNumber({
					provider: 'weixin',
					success: function(infoRes) {
						console.log("获取手机号：",infoRes)
					},
				})
			},

　　　　　　//登录
			login1() {
				let _this = this;
				
				uni.showLoading({
					title: '登录中...'
				});
			 
			   // 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let code = loginRes.code;
						if (!_this.isCanUse) {
							//非第一次授权获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
 　　　　　　　　　　　　　　　　　　　　　　//获取用户信息后向调用信息更新方法
									_this.nickName = infoRes.userInfo.nickName; //昵称
									_this.sex = infoRes.userInfo.gender;
									_this.province = infoRes.userInfo.province;
									_this.city = infoRes.userInfo.city;
									_this.country = infoRes.userInfo.country;
									_this.avatarUrl = infoRes.userInfo.avatarUrl; //头像
									
									_this.updateUserInfo();//调用更新信息方法
								}
							});
						}
						
						//2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
						_this.$http.post("/user/wxOpenId",{
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
							CODE: code,
						}).then((e)=>{
							if(e.statusCode == 200 && e.data.STATE== '0'){
								uni.setStorageSync("OPENID",e.data.OBJECT.OPENID)
								uni.hideLoading();
							}
						}).catch((err)=>{
							console.log("请求超时",err);
						})
						// uni.request({
						// 	
						// 	url: getApp().globalData.http+'/user/wxLogin',
						// 	data: {
						// 		CODE: code,
						// 		SIGNATURE : uni.getStorageSync("SIGNATURE"),
						// 	},
						// 	method: 'GET',
						// 	header: {
						// 		'content-type': 'application/json'
						// 	},
						// 	success: (res) => {
						// 		//openId、或SessionKdy存储//隐藏loading
						// 		uni.hideLoading();
						// 	}
						// });
					},
				});
			},
		 //向后台更新信息
			updateUserInfo() {
				let _this = this;
				_this.$http.post("/user/updateUserInfoWx",{
					SIGNATURE : uni.getStorageSync("SIGNATURE"),
					OPENID:uni.getStorageSync("OPENID"),
					NICKNAME: _this.nickName,
					SEX:_this.sex,
					PROVINCE:_this.province,
					CITY:_this.city,
					COUNTRY:_this.country,
					HEADIMGURL: _this.avatarUrl,
				}).then((e)=>{
					if(e.statusCode == 200 && e.data.STATE== '0'){
						console.log("更新用户信息成功,并且登录成功",)
						_this.login(e.data.OBJECT);
						uni.reLaunch({//信息更新成功后跳转到小程序首页
							url: '/pages/index/index'
						});
					}else if(e.statusCode == 200 && e.data.STATE== '-1'){
						console.log("跳转注册")
						uni.navigateTo({
							url: '/pages/public/wechatLogin/regWx'
						});
					}
				}).catch((err)=>{
					console.log("请求超时",err);
				})
				// uni.request({
				// 	url:getApp().globalData.http+'/user/updateUserInfoWx',//服务器端地址
				// 	data: {
				// 		// appKey: this.$store.state.appKey,
				// 		// customerId: _this.customerId,
				// 		NICKNAME: _this.nickName,
				// 		SEX:_this.sex,
				// 		PROVINCE:_this.province,
				// 		CITY:_this.city,
				// 		COUNTRY:_this.country,
				// 		HEADIMGURL: _this.avatarUrl,
				// 		SIGNATURE : uni.getStorageSync("SIGNATURE"),
				// 	},
				// 	method: 'POST',
				// 	header: {
				// 		'content-type': 'application/x-www-form-urlencoded'
				// 	},
				// 	success: (res) => {
				// 		if (res.data.STATE== '0'&&res.statusCode == 200) {
				// 			
				// 			// uni.reLaunch({//信息更新成功后跳转到小程序首页
				// 			// 	url: '/pages/index/index'
				// 			// });
				// 		}
				// 	}
				//    
				// });
				
			}
		},
	}
</script>

<style>
	.header {
	        margin: 90rpx 0 90rpx 50rpx;
	        border-bottom: 1px solid #ccc;
	        text-align: center;
	        width: 650rpx;
	        height: 300rpx;
	        line-height: 450rpx;
	    }
	
	    .header image {
	        width: 200rpx;
	        height: 200rpx;
	    }
	
	    .content {
	        margin-left: 50rpx;
	        margin-bottom: 90rpx;
	    }
	
	    .content text {
	        display: block;
	        color: #9d9d9d;
	        margin-top: 40rpx;
	    }
	
	    .bottom {
	        border-radius: 80rpx;
	        margin: 70rpx 50rpx;
	        font-size: 35rpx;
	    }
</style>
